package com.leetcode.LC;

public class LC1232 {
    public boolean checkStraightLine(int[][] coordinates) {
        if (coordinates.length == 2) {
            return true;
        }
        //比较斜率无法处理分母为0的情况
        double k = (coordinates[1][1] - coordinates[0][1]) / (coordinates[1][0] - coordinates[0][0]);

        for (int i = 0; i < coordinates.length - 1; i++) {
            if (k != toK(coordinates[i], coordinates[i + 1])) {
                return false;
            }
        }
        return true;
    }

    public double toK(int[] coordinate1, int[] coordinate2) {

        return (coordinate2[1] - coordinate1[1]) / (coordinate2[0] - coordinate1[0]);

    }


    public boolean checkStraightLine2(int[][] coordinates) {
        //分母为0，选择变换相乘
        if (coordinates.length == 2) {
            return true;
        }
        for (int i = 0; i < coordinates.length - 2; i++) {
            if (((coordinates[i + 1][1] - coordinates[i][1]) * ((coordinates[i + 2][0] - coordinates[i + 1][0]))) != ((coordinates[i + 1][0] - coordinates[i][0]) * (coordinates[i + 2][1] - coordinates[i + 1][1]))) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(new LC1232().checkStraightLine2(new int[][]{{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}}));
    }
}
